<?php
// +----------------------------------------------------------------------
// | TYCMS [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2016 http://www.rainfer.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: aliang <461321026@qq.com>
// +----------------------------------------------------------------------
namespace app\common\controller;

use think\Controller;
use think\Lang;
use think\captcha\Captcha;
use think\Db;

class Common extends Controller
{
    // Request实例
	protected $lang;
	protected $yonghu = [
	    'qq'  =>'',
	    'tel' =>'',
	    'qrcode'=>'',
	];
	protected function _initialize()
    {
		parent::_initialize();
        if (!defined('__ROOT__')) {
            $_root = rtrim(dirname(rtrim($_SERVER['SCRIPT_NAME'], '/')), '/');
            define('__ROOT__', (('/' == $_root || '\\' == $_root) ? '' : $_root));
        }
		if (!file_exists(ROOT_PATH.'data/install.lock')) {
            //不存在，则进入安装
            header('Location: ' . url('install/Index/index'));
            exit();
        }
        if (!defined('MODULE_NAME')){define('MODULE_NAME', $this->request->module());}
        if (!defined('CONTROLLER_NAME')){define('CONTROLLER_NAME', $this->request->controller());}
        if (!defined('ACTION_NAME')){define('ACTION_NAME', $this->request->action());}
		// 多语言
		if(config('lang_switch_on')){
			$this->lang=Lang::detect();
		}else{
			$this->lang=config('default_lang');
		}
		$this->assign('lang',$this->lang);
		
		
		
		$ShopUrl = url("home/shop/index");
		$procode = cookie('procode') ? cookie('procode'):'';//input('procode') ? htmlspecialchars(strip_tags(input('procode/s'))):'';
		
		$uid = session('hid');
		$jprocode = $tprocode = '';
		if(!empty($uid)){
		   $jInfo    =  Db::name("member_list")->where("member_list_id",$uid)->find();
		   $jprocode =  $jInfo['code'];//$jInfo['procode'];
		   $procode  =  !empty($jprocode) ? $jprocode:$procode;
		   $tprocode =  $jInfo['code'];
		}
		 
		if(!empty($procode)){
		   cookie('procode',$procode,3600*24*7);		   
		   //新增输出yonghu部分 推广用户的vcode值
		   $yh_info      = Db::name("member_list")->field('qq,qrcode,member_list_tel as tel,member_list_groupid as groupid')->fetchsql(false)->where("code = '{$procode}'")->find();
		   //dump($yh_info);exit;
		   if(!empty($yh_info)  && $yh_info['groupid'] > 1){
		      $this->yonghu = $yh_info;
		      $ShopUrl      = url("home/shop/index",['procode'=>$tprocode]);
		   }elseif(!empty($yh_info) && !empty($jprocode)) {
		      $this->yonghu = $yh_info;
		      $ShopUrl      = url("home/shop/index",['procode'=>$tprocode]);
		   }  
		} 		
		$setting = config('options');
		$this->yonghu = [
		       'qq'    => !empty($this->yonghu['qq'])     ? $this->yonghu['qq']:(isset($setting['site_qq'])  ? $setting['site_qq']:''),
		       'tel'   => !empty($this->yonghu['tel'])    ? $this->yonghu['tel']:(isset($setting['site_tel']) ? $setting['site_tel']:''),
		       'qrcode'=> !empty($this->yonghu['qrcode']) ? $this->yonghu['qrcode']:(isset($setting['wx_pic'])   ? $setting['wx_pic']:''),
	    ]; 
		$this->assign('web',$setting);
		$this->yonghu['qrcode'] = '/'.trim($this->yonghu['qrcode'],'/');
		$this->assign('yonghu',$this->yonghu);
		$this->assign('shop_url',$ShopUrl);
	}
    //空操作
    public function _empty()
    {
        $this->error(lang('operation not valid'));
    }
	protected function verify_build($id)
	{
		//ob_end_clean();
		$verify = new Captcha (config('verify'));
		return $verify->entry($id);
	}
	protected function verify_check($id)
	{
		$verify =new Captcha ();
		if (!$verify->check(input('verify'), $id)) {
			$this->error(lang('verifiy incorrect'),url(MODULE_NAME.'/Login/login'));
		}
	}
    protected function check_admin_login(){
		return model('admin/Admin')->is_login();
    }
}